if(__add_gitinfo)
  return()
endif()
set(__add_gitinfo ON)

execute_process(
  COMMAND           ${GIT_EXECUTABLE} log -1 --format=%h
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  OUTPUT_VARIABLE   OMVLL_COMMIT_HASH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
  COMMAND           ${GIT_EXECUTABLE} rev-list --count ${OMVLL_COMMIT_HASH}
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  OUTPUT_VARIABLE   OMVLL_COMMIT_COUNT
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
  COMMAND           ${GIT_EXECUTABLE} describe --abbrev=0 --tags HEAD --always
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  OUTPUT_VARIABLE   OMVLL_GIT_TAG
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
  COMMAND           ${GIT_EXECUTABLE} tag --list --points-at=HEAD
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  OUTPUT_VARIABLE   OMVLL_GIT_COMMIT_TAGGED
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

execute_process(
  COMMAND           ${GIT_EXECUTABLE} rev-parse --abbrev-ref HEAD
  WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
  OUTPUT_VARIABLE   OMVLL_GIT_BRANCH
  OUTPUT_STRIP_TRAILING_WHITESPACE
)

string(COMPARE NOTEQUAL "${OMVLL_GIT_COMMIT_TAGGED}" "" OMVLL_IS_TAGGED)
string(REGEX MATCHALL "([0-9]+)" VERSION_STRING "${OMVLL_GIT_TAG}")

message(STATUS "Tagged: ${OMVLL_IS_TAGGED}")
if (${OMVLL_IS_TAGGED})
  message(STATUS "Tag: ${OMVLL_GIT_TAG}")
else()
  if(OMVLL_GIT_BRANCH MATCHES "^release-")
    string(REGEX MATCHALL "([0-9]+)" VERSION_STRING "${OMVLL_GIT_BRANCH}")
    message(STATUS "${VERSION_STRING}")
  endif()
endif()
message(STATUS "Current branch: ${OMVLL_GIT_BRANCH}")
message(STATUS "Current commit: ${OMVLL_COMMIT_HASH}")


if (VERSION_STRING)
  list(GET VERSION_STRING 0 OMVLL_VERSION_MAJOR)
  list(GET VERSION_STRING 1 OMVLL_VERSION_MINOR)
  list(GET VERSION_STRING 2 OMVLL_VERSION_PATCH)

  if (NOT ${OMVLL_IS_TAGGED})
    if(OMVLL_GIT_BRANCH MATCHES "^release-")
      message(STATUS "Release branch")
    else()
      MATH(EXPR OMVLL_VERSION_MINOR "${OMVLL_VERSION_MINOR}+1")
      set(OMVLL_VERSION_PATCH 0)
    endif()
  endif()
else()
  set(OMVLL_VERSION_MAJOR 0)
  set(OMVLL_VERSION_MINOR 0)
  set(OMVLL_VERSION_PATCH 0)
endif()

